﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IdeaNMR.Web.Core;
using System.Text.RegularExpressions;

namespace IdeaNMR.Web
{
    public partial class SpectrometersTable : ListHardObjects_BaseTable<Spectrometer>
    {

        protected override Item[] LoadRawData()
        {
            IdeaNMRdbContainer inmr = new IdeaNMRdbContainer();
            var result = from s in inmr.BaseObjects.OfType<Spectrometer>()
                         where s.User.Username == ScrewTurnWikiFacade.Username || s.IsPrivate == false
                         group s by s.FirstVersion.Id into gr
                         let res = gr.FirstOrDefault(x => x.Datetime == gr.Max(y => y.Datetime))
                         select new Item
                         {
                             Obj = res
                         };
            return result.ToArray();
        }

        protected override Item[] FilterRawData()
        {
            IEnumerable<Item> result = base.FilterRawData();

            if (this.SearchTerms.Length > 0)
            {
                var res1 = from r in this.RawData
                           where 
                                Regex.IsMatch(r.Obj.Frequency.ToString().ToLowerInvariant(), this.SearchRegEx)
                           select r;
                result = result.Union(res1);
            }

            return result.ToArray();
        }
    }
}